#!/usr/bin/env bash

set +u

if [[ $BUILDKITE_PULL_REQUEST != "false" ]]; then
  if [[ $BUILDKITE_LABEL == ":service_dog: Linting" ]]; then
    echo "--- :go::service_dog: Provide in-line commentary for pull request"
    reviewdog -reporter=github-pr-review
  fi
fi

if [[ ! $BUILDKITE_BRANCH =~ ^(v.*) ]] && [[ $BUILDKITE_COMMAND_EXIT_STATUS == 0 ]]; then
  if [[ $BUILDKITE_LABEL == ":hammer_and_wrench: Unit Test" ]] || [[ $BUILDKITE_LABEL =~ ":selenium:" ]]; then
    echo "--- :codecov: Upload coverage reports"
    if [[ $BUILDKITE_AGENT_META_DATA_CODECOV == "verbose" ]]; then
      BUILDKITE_AGENT_META_DATA_CODECOV="-v"
    fi
    bash <(curl -s --connect-timeout 10 --retry 10 --retry-max-time 0 https://codecov.io/bash) -Z -c -s 'coverage*.txt' -F backend ${BUILDKITE_AGENT_META_DATA_CODECOV}
    if [[ $BUILDKITE_LABEL =~ ":selenium:" ]]; then
      cd web && yarn report
    fi
    bash <(curl -s --connect-timeout 10 --retry 10 --retry-max-time 0 https://codecov.io/bash) -Z -c -f '!*.go' -f '!*.zst' -F frontend ${BUILDKITE_AGENT_META_DATA_CODECOV}
  fi
fi

if [[ $BUILDKITE_LABEL =~ ":selenium:" ]] || [[ $BUILDKITE_LABEL =~ ":docker: Build Image" ]]; then
  CONTAINERS=$(docker ps -a -q)
  if [[ ${CONTAINERS} != "" ]]; then
    echo "--- :docker: Remove lingering containers"
    docker rm -f ${CONTAINERS}
  fi
fi

if [[ $BUILDKITE_LABEL == ":docker: Image Deployments" ]]; then
  cat .buildkite/annotations/artifacts | buildkite-agent annotate --style "success" --context "ctx-success"
fi

if [[ $BUILDKITE_LABEL =~ ":docker: Deploy" ]]; then
  docker logout
fi

if [[ $BUILDKITE_LABEL == ":docker: Deploy Manifests" ]] && [[ $BUILDKITE_BRANCH == "master" ]] && [[ $BUILDKITE_PULL_REQUEST == "false" ]]; then
  echo "--- :docker: Removing tags for deleted branches"
  anontoken=$(curl -fsL --retry 3 'https://auth.docker.io/token?service=registry.docker.io&scope=repository:authelia/authelia:pull' | jq -r .token)
  authtoken=$(curl -fs --retry 3 -H "Content-Type: application/json" -X "POST" -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
  for BRANCH_TAG in $(dockerbranchtags=$(curl -fsL --retry 3 -H "Authorization: Bearer ${anontoken}" https://registry-1.docker.io/v2/authelia/authelia/tags/list | jq -r '.tags[] | select(startswith("PR") | not)' | \
  sed -r '/^(latest|master|develop|v.*|([[:digit:]]+)\.?([[:digit:]]+)?\.?([[:digit:]]+)?)|(amd64|arm32v7|arm64v8)$/d' | sort) && \
  githubbranches=$(curl -fs --retry 3 https://api.github.com/repos/authelia/authelia/branches | jq -r '.[].name' | sort) && \
  comm -23 <(echo "${dockerbranchtags}") <(echo "${githubbranches}")); do
    echo "Removing tag ${BRANCH_TAG}"
    curl -fsL --retry 3 -o /dev/null -X "DELETE" -H "Authorization: JWT ${authtoken}" https://hub.docker.com/v2/repositories/authelia/authelia/tags/${BRANCH_TAG}/
  done
  echo "--- :docker: Removing tags for merged or closed pull requests"
  for PR_TAG in $(dockerprtags=$(curl -fsL --retry 3 -H "Authorization: Bearer ${anontoken}" https://registry-1.docker.io/v2/authelia/authelia/tags/list | jq -r '.tags[] | select(startswith("PR"))' | \
  sed -r '/(amd64|arm32v7|arm64v8)$/d' | sort) && \
  githubprs=$(curl -fs --retry 3 https://api.github.com/repos/authelia/authelia/pulls | jq -r '.[].number' | sed -e 's/^/PR/' | sort) && \
  comm -23 <(echo "${dockerprtags}") <(echo "${githubprs}")); do
    echo "Removing tag ${PR_TAG}"
    curl -fsL --retry 3 -o /dev/null -X "DELETE" -H "Authorization: JWT ${authtoken}" https://hub.docker.com/v2/repositories/authelia/authelia/tags/${PR_TAG}/
  done
fi